﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;
using RssWindowsPhone.ServiceReference;

namespace RssWindowsPhone
{

    public partial class RssReader : PhoneApplicationPage
    {
        String user;
        String password;
        String link;
        String id;
        String title;

        public RssReader()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            NavigationContext.QueryString.TryGetValue("User", out this.user);
            NavigationContext.QueryString.TryGetValue("Password", out this.password);
            NavigationContext.QueryString.TryGetValue("Title", out this.title);
            NavigationContext.QueryString.TryGetValue("Id", out this.id);
            NavigationContext.QueryString.TryGetValue("Link", out this.link);

            ServiceRssClient client = new ServiceRssClient();
            LoginModel log = new LoginModel()
            {
                Password = this.password,
                UserName = this.user,
                RememberMe = false
            };

            Flux toGet = new Flux()
            {
                ID = System.Convert.ToInt32(this.id),
                Link = this.link,
                Title = this.title

            };
            client.GetRssAsync(log, toGet);
            client.GetRssCompleted += new EventHandler<GetRssCompletedEventArgs>(client_GetRssCompleted);
        }
          
        void client_GetRssCompleted(object sender, GetRssCompletedEventArgs e)
        {
            List<Rss> articles = e.Result.ToList();

            String content = "";
            String sep = "<br />";
        

            content += "<html><body>";
            content += "<style type=\"text/css\">body{color: white; background: black;} h1{font-size: 20px} a{color: #9d9d3d}</style>";
            foreach (Rss article in articles)
            {
                content += "<h1>"+article.Title+"</h1>";
                content += article.Description+sep;
                content += "<a href='"+article.Link+"'>Read More</a><br /><br /><br />";
            }

            content += "</body></html>";
            this.rssReader.NavigateToString(content);

        }

       

    }
}